package com.ys.peony.ui.adapter;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ys.peony.R;
import com.ys.peony.app.BaseApplication;
import com.ys.peony.app.YSConstant;
import com.ys.peony.enums.BusinessDetailStatus;
import com.ys.peony.model.JiekuanrenModel;
import com.ys.peony.ui.activity.AddClientInfoActivity;
import com.ys.peony.ui.activity.BusiInfoActivity;
import com.ys.peony.ui.activity.JiekuanrenListActivity;
import com.ys.peony.ui.view.zlistview.adapter.BaseSwipeAdapter;
import com.ys.peony.ui.view.zlistview.enums.DragEdge;
import com.ys.peony.ui.view.zlistview.enums.ShowMode;
import com.ys.peony.ui.view.zlistview.listener.SimpleSwipeListener;
import com.ys.peony.ui.view.zlistview.widget.ZSwipeItem;
import com.ys.peony.ui.widget.TipAlertDialog;

import java.util.List;

public class JiekuanrenZListViewAdapter extends BaseSwipeAdapter {
    protected static final String TAG = JiekuanrenZListViewAdapter.class.getSimpleName();
    private JiekuanrenListActivity context;
    private List<JiekuanrenModel> data;

    public JiekuanrenZListViewAdapter(JiekuanrenListActivity context) {
        this.context = context;
    }

    @Override
    public int getCount() {
        return data == null ? 0 : data.size();
    }

    @Override
    public JiekuanrenModel getItem(int position) {
        return data == null ? null : data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getSwipeLayoutResourceId(int position) {
        return R.id.swipe_item;
    }

    @Override
    public View generateView(int position, ViewGroup parent) {
        return context.getLayoutInflater().inflate(R.layout.jiekuanren_item_layout,
                parent, false);
    }

    @Override
    public void fillValues(final int position, final View convertView) {
        final ZSwipeItem swipeItem = (ZSwipeItem) convertView
                .findViewById(R.id.swipe_item);
        LinearLayout rightLL = (LinearLayout) convertView.findViewById(R.id.right_ll);
        TextView detailBtn = (TextView) convertView.findViewById(R.id.item_detail_tv);
        TextView delBtn = (TextView) convertView.findViewById(R.id.item_delete_tv);
        TextView nameTV = (TextView) convertView.findViewById(R.id.jiekuanren_name_tv);
        TextView carBranchTV = (TextView) convertView.findViewById(R.id.jiekuanren_car_tv);
        TextView clientClassifyTV = (TextView) convertView.findViewById(R.id.jiekuanren_kehu_tv);
        TextView carNumTV = (TextView) convertView.findViewById(R.id.jiekuanren_carnum_tv);
        TextView ywStatusTV = (TextView) convertView.findViewById(R.id.jiekuanren_status_tv);
        TextView ywNumTV = (TextView) convertView.findViewById(R.id.jiekuanren_yewunum_tv);
        RelativeLayout basicInfoBtn = (RelativeLayout) convertView.findViewById(R.id.basicinfo_rl);
        RelativeLayout carInfoBtn = (RelativeLayout) convertView.findViewById(R.id.diyawu_rl);
        TextView basicInfoTV = (TextView) convertView.findViewById(R.id.basicinfo_tv);
        TextView carInfoTV = (TextView) convertView.findViewById(R.id.diyawu_tv);
        final JiekuanrenModel item = data.get(position);
        if (item == null) {
            return;
        }

        //状态是0、5、7的时候信息是可以修改的，其他状态的话要把基本信息编辑和抵押物编辑两个按钮质成灰色
        nameTV.setText(BaseApplication.getInstance().getResources().getString(R.string.name_lable) + item.getClientName());
        if(item.getVehicleList()!=null &&item.getVehicleList().size()>0){
            carBranchTV.setText(BaseApplication.getInstance().getResources().getString(R.string.jiekuanren_car) + item.getVehicleList().get(item.getVehicleList().size() - 1).getBrand_name());
            carNumTV.setText(BaseApplication.getInstance().getResources().getString(R.string.jiekuanren_carnum) + item.getVehicleList().get(item.getVehicleList().size() - 1).getPlate_number());
        }else{
            carBranchTV.setText(BaseApplication.getInstance().getResources().getString(R.string.jiekuanren_car) + "无");
            carNumTV.setText(BaseApplication.getInstance().getResources().getString(R.string.jiekuanren_carnum) + "无");
        }
        clientClassifyTV.setText(BaseApplication.getInstance().getResources().getString(R.string.jiekuanren_kehu) + item.getClientClassfyName());
        ywStatusTV.setText(BaseApplication.getInstance().getResources().getString(R.string.jiekuanren_yewu_status) + BusinessDetailStatus.getStatus(item.getLoanStatus()));
        ywNumTV.setText(BaseApplication.getInstance().getResources().getString(R.string.jiekuanren_yewu_num) + item.getSerialNumber());
        carInfoTV.setTextColor(context.getResources().getColor(R.color.color_a6a4a4));
        if("0".equals(item.getLoanStatus())||"5".equals(item.getLoanStatus())||"7".equals(item.getLoanStatus())){
            basicInfoTV.setTextColor(context.getResources().getColor(R.color.color_5A93F8));

        }else{
            basicInfoTV.setTextColor(context.getResources().getColor(R.color.color_a6a4a4));
        }
        swipeItem.setShowMode(ShowMode.PullOut);
        swipeItem.setDragEdge(DragEdge.Right);

        swipeItem.addSwipeListener(new SimpleSwipeListener() {
            @Override
            public void onOpen(ZSwipeItem layout) {
//                LogUtil.i(TAG, "打开:" + position);
            }

            @Override
            public void onClose(ZSwipeItem layout) {
//                LogUtil.i(TAG, "关闭:" + position);
            }

            @Override
            public void onStartOpen(ZSwipeItem layout) {
//                LogUtil.i(TAG, "准备打开:" + position);
            }

            @Override
            public void onStartClose(ZSwipeItem layout) {
//                LogUtil.i(TAG, "准备关闭:" + position);
            }

            @Override
            public void onHandRelease(ZSwipeItem layout, float xvel, float yvel) {
//                LogUtil.i(TAG, "手势释放");
            }

            @Override
            public void onUpdate(ZSwipeItem layout, int leftOffset,
                                 int topOffset) {
//                LogUtil.i(TAG, "位置更新");
            }
        });

        detailBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent it=new Intent(context, BusiInfoActivity.class);
                it.putExtra("loanId",item.getId());
                context.startActivity(it);
                swipeItem.close();
            }
        });
        delBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                if("0".equals(item.getLoanStatus())||"5".equals(item.getLoanStatus())||"7".equals(item.getLoanStatus())) {
                    context.delJiekuanrenItem(item.getId());
                    swipeItem.close();
                }else{
                    swipeItem.close();
                    TipAlertDialog dialog=new TipAlertDialog(context);
                }
            }
        });
        basicInfoBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if("0".equals(item.getLoanStatus())||"5".equals(item.getLoanStatus())||"7".equals(item.getLoanStatus())){
                    Intent it=new Intent(context, AddClientInfoActivity.class);
                    it.putExtra("loanId",item.getId());
                    it.putExtra("addFlag",false);
                    context.startActivityForResult(it, YSConstant.ACTIVITY_ADD_YW_CLIENT);

                }else{
                    TipAlertDialog dialog=new TipAlertDialog(context);
                }

            }
        });

        carInfoBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                /*if("0".equals(item.getLoanStatus())||"5".equals(item.getLoanStatus())||"7".equals(item.getLoanStatus())){
                    Intent it=new Intent(context, DywListActivity.class);
                    it.putExtra("loanId",item.getId());
                    context.startActivityForResult(it, YSConstant.ACTIVITY_ADD_YW_CLIENT);

                }else{
                    TipAlertDialog dialog=new TipAlertDialog(context);
                }*/

            }
        });
    }

    public void setData(List<JiekuanrenModel> date) {
        this.data = date;
        notifyDataSetChanged();
    }


}
